package rabc.service;

import rabc.domain.dto.DeptDto;
import rabc.domain.dto.UserDto;
import rabc.domain.entity.Dept;
import rabc.domain.entity.Role;
import rabc.domain.entity.User;
import rabc.domain.vo.DeptVo;
import rabc.domain.vo.Result;
import rabc.domain.vo.UserVo;

import java.util.Arrays;
/**
 * @author 朱磊
 * @since 2025/5/17 20:07
 */
public class  UserService {
    public static int DATA_SIZE = 100;
    public static User[] data = new User[DATA_SIZE+1];
    public static int idCounter = 1;

    public static boolean existsById(int id){
        return id>=1&&id<idCounter&&data[id]!=null;
    }
    public  static UserVo convertToVo(User user){
        String deptName = "";
        Dept dept = Deptservice.findById(user.deptId);
        if(dept!=null){
            deptName = dept.name;
        }
        String roleName = "";
        Role role = Roleservice.findById(user.roleId);
        if(role!=null){
            roleName = role.name;
        }
        return new UserVo(user.deptId,deptName,user.email ,user.enable,user.id,user.mobile,user.name, user.roleId,roleName ,user.username);
    }
    public  static User createEntity(UserDto dto, int id){
        User user = new User(dto.username,dto.name,dto.mobile,dto.email,dto.deptId,dto.roleId);
        user.id = id;
        return user;
    }
    public static void updataEntity(UserDto dto,User user){
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
    public static boolean deptInUse(int deptId){
        boolean inUse = false;
        for(User user: data ){
            if(user.deptId == deptId&&user !=null){
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    public static boolean roleInUse(int roleId){
        boolean inUse = false;
        for(User user: data ){
            if(user.roleId == roleId&&user !=null){
                inUse = true;
                break;
            }
        }
        return inUse;
    }
    public static Result list(){
        UserVo[] temp = new UserVo[DATA_SIZE];
        int length=0;
        for (int i = 1; i < idCounter; i++) {

            if (data[i] != null) {
                temp[i] = convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp,length));
    }
    public static Result create(UserDto dto){
        if (idCounter > DATA_SIZE){
            return Result.failure("数据已满");
        }
        if(!Deptservice.existsById(dto.deptId)) {
            return Result.failure("部门不存在");
        }
        if(!Roleservice.existsById(dto.roleId)) {
            return Result.failure("角色不存在");
        }

        User user = createEntity(dto,idCounter++);
        data[user.id] = user;
        return Result.success("创建成功",null);
    }
    public static Result update(UserDto dto,int id) {
        if (!existsById(id)){
            return Result.failure("用户不存在");
        }
        if(!Deptservice.existsById(dto.deptId)) {
            return Result.failure("部门不存在");
        }
        if(!Roleservice.existsById(dto.roleId)) {
            return Result.failure("角色不存在");
        }
        User user = data[id];
        updataEntity(dto,user);
        return Result.success("更新成功",null);
    }
    public static Result toggle (int id) {
        if(!existsById(id)){
            return Result.failure("用户不存在,切换状态失败");
        }
        data[id].enable = !data[id].enable;
        return Result.success(data[id].enable ? "启用成功" : "停用成功",null);
    }


}
